package com.xtoad.ecms.baseinfo.controller;

import com.xtoad.ecms.baseinfo.dto.RoleDTO;
import com.xtoad.ecms.baseinfo.service.IRoleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 角色控制器
 *
 * @author xtoad
 * @date 2020/05/29
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    /**
     * 根据id查询角色信息
     *
     * @param id 角色id
     * @return 查询结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RoleDTO getRoleById(@PathVariable final Long id) {
        RoleDTO role = roleService.getRoleById(id);

        return role;
    }

    /**
     * 查询全部角色信息，不分页
     *
     * @return 查询结果
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<RoleDTO> getAllRole() {
        List<RoleDTO> roles = roleService.getAllRole();

        return roles;
    }

    /**
     * 根据角色编码模糊查询角色信息
     *
     * @param code 角色编码
     * @return 查询结果
     */
    @RequestMapping(value = "/getByCode/{code}", method = RequestMethod.GET)
    public List<RoleDTO> getUserWithCode(@PathVariable final String code) {
        List<RoleDTO> roles = roleService.getRoleByCode(code);

        return roles;
    }

    /**
     * 根据角色名称模糊查询角色信息
     *
     * @param name 角色名称
     * @return 查询结果
     */
    @RequestMapping(value = "/getByName/{name}", method = RequestMethod.GET)
    public List<RoleDTO> getUserWithName(@PathVariable final String name) {
        List<RoleDTO> roles = roleService.getRoleByName(name);

        return roles;
    }

    /**
     * 新增角色信息
     *
     * @param roleDTO 角色信息
     * @return 更新结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RoleDTO insertRole(@Valid @RequestBody final RoleDTO roleDTO) {
        RoleDTO role = roleService.insert(roleDTO);

        return role;
    }

    /**
     * 根据角色id删除角色
     *
     * @param id 删除对象id
     * @return 更新结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Boolean updateRole(@PathVariable final Long id) {
        roleService.deleteById(id);

        return true;
    }

    /**
     * 根据角色id修改角色信息
     *
     * @param roleDTO 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public RoleDTO updateRole(@PathVariable final Long id, @Valid @RequestBody final RoleDTO roleDTO) {
        roleDTO.setId(id);
        RoleDTO role = roleService.update(roleDTO);

        return role;
    }

    /**
     * 批量新增角色信息
     *
     * @param roles 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public List<RoleDTO> batchAddRole(@Valid @RequestBody final List<RoleDTO> roles) {
        List<RoleDTO> roleDTOS = roleService.batchUpdate(roles);

        return roleDTOS;
    }

    /**
     * 批量删除角色信息
     *
     * @param roles 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/delete", method = RequestMethod.POST)
    public Boolean batchDelRole(@Valid @RequestBody final List<RoleDTO> roles) {
        roleService.batchDelete(roles);

        return true;
    }

    /**
     * 批量修改角色信息
     *
     * @param roles 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/modify", method = RequestMethod.POST)
    public List<RoleDTO> batchUpdRole(@Valid @RequestBody final List<RoleDTO> roles) {
        List<RoleDTO> roleDTOS = roleService.batchUpdate(roles);

        return roleDTOS;
    }
}
